package cn.cold.album.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class AlbumUserDetail implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final UserInfo userInfo;

	private final List<GrantedAuthority> authorities;

	public AlbumUserDetail(UserInfo userInfo) {
		this.userInfo = userInfo;
		if (userInfo != null) {
			authorities = new ArrayList<GrantedAuthority>();
			if (userInfo.getUserType() != null && userInfo.getUserType().getAuthorities() != null) {
				for (String item : userInfo.getUserType().getAuthorities()) {
					authorities.add(new SimpleGrantedAuthority(item));
				}
			}
		} else {
			authorities = null;
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return userInfo.getPassword();
	}

	@Override
	public String getUsername() {
		return userInfo.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return userInfo.isEnabled();
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return userInfo.isEnabled();
	}

}
